package com.H1023.controller;

import com.H1023.pojo.Book;
import com.H1023.service.BookIOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;

/**
 * @author 小强
 * @create 2021-10-11 23:40
 */
@Controller
@RequestMapping("/bookIO")
public class BookIOController {

    /**
     * 自动装配属性
     * bookService用于实现出入库业务的操作
     */
    @Autowired
    @Qualifier("bookIOServiceImpl")
    private BookIOService bookIOService;


    /**
     * 请求：跳转所有书籍页面
     * @param model 封装用于返回视图的数据
     * @return allBooK页面（交给视图解析器解析）
     */
    @RequestMapping("/toAllIOBook")
    public String allIOBook(Model model) {
        List<Book> books = bookIOService.queryAllBook();
        model.addAttribute("list", books);
        return "allIOBook";
    }


    /**
     * 请求：/toStoreBook->请求入库书籍（添加书籍）
     * @return storeBook入库书籍页面（视图解析器解析）
     */
    @RequestMapping("/toStoreBook")
    public String toStoreBook() {
        return "storeBook";
    }


    /**
     * 请求：入库书籍
     * @param book 接收前端传来的书籍信息
     * @param request 前端请求
     * @param img 接收前端书籍图片信息（文件格式）
     * @return  "redirect:/bookIO/toAllIOBook"  重定向到主页（所有书籍页面）
     * @throws IOException
     */
    @RequestMapping("/storeBook")
    public String storeBook(Book book,HttpServletRequest request, @RequestParam("img") CommonsMultipartFile img)
            throws IOException {
        String uploadFileName = img.getOriginalFilename();  //图片文件名
        book.setPic(uploadFileName);  //保存图片文件名

        if ("".equals(uploadFileName)){
            return "redirect:/bookIO/toAllIOBook";
        }
        System.out.println("上传文件名 : "+uploadFileName);

        int i = bookIOService.storeBook(book);

        if (i == 1) {
            System.out.println("入库成功！");
        } else
            System.out.println("入库失败！");

        return "redirect:/bookIO/toAllIOBook";
    }


    /**
     * 请求：/outBook ->书籍出库请求
     * @param id 接收前端传来的要出库的书籍id
     * @return  "redirect:/bookIO/toAllIOBook" 重定向到所有书籍页面
     */

    @RequestMapping("/outBook")
    public String outBook(int id) {
        int i = bookIOService.outBookByID(id);
        return "redirect:/bookIO/toAllIOBook";
    }


    /**
     * 请求：/toBookDetail -> 请求书籍详情页面
     * 先从数据库取得书籍信息，在对书籍类型封装字段值
     * 保存到model，传到前端页面显示
     * @param model 保存需要传到前端页面的数据
     * @param id 接收前端传来的书籍ID
     * @return "bookDetail" 转发到书籍详情页面
     */

    @RequestMapping("/toBookDetail")
    public String toBookDetail(Model model, int id) {
        Book book = bookIOService.queryBookByID(id);
        String type1 = null;
        String type2 = null;
        String type3 = null;
        String bookState = null;
        switch (book.getTypeID1()) {
            case 1:
                type1 = "经典著作";
                break;
            case 2:
                type1 = "青春校园";
                break;
            case 3:
                type1 = "历史文学";
                break;
        }
        switch (book.getTypeID2()) {
            case 1:
                type2 = "经典著作";
                break;
            case 2:
                type2 = "青春校园";
                break;
            case 3:
                type2 = "历史文学";
                break;
        }
        switch (book.getTypeID3()) {
            case 1:
                type3 = "经典著作";
                break;
            case 2:
                type3 = "青春校园";
                break;
            case 3:
                type3 = "历史文学";
                break;
        }

        switch (book.getBookState()) {
            case 0:
                bookState = "已出库";
                break;
            case 1:
                bookState = "未出库";
                break;
        }
        System.out.println("----------图片名称为------"+book.getPic());
        model.addAttribute("bookID", id);
        model.addAttribute("typeID1", type1);
        model.addAttribute("typeID2", type2);
        model.addAttribute("typeID3", type3);
        model.addAttribute("bookState", bookState);
        model.addAttribute("pic", book.getPic());
        return "bookDetail";
    }


    /**
     * 请求：/toOutedBookDetail -> 请求查看已出库书籍详情
     * @param model1 封装需要转发到前端的数据
     * @param id 接收前端传来的书籍ID
     * @return “outedBookDetail” ->转发到已出库书籍详情页面
     */
    @RequestMapping("/toOutedBookDetail")
    public String toOutedBookDetail(Model model1, int id) {
        Book book1 = bookIOService.queryOutedBookByID(id);
        String outedType1 = null;
        String outedType2 = null;
        String outedType3 = null;
        String outedBookState = null;
        switch (book1.getTypeID1()) {
            case 1:
                outedType1 = "经典著作";
                break;
            case 2:
                outedType1 = "青春校园";
                break;
            case 3:
                outedType1 = "历史文学";
                break;
        }
        switch (book1.getTypeID2()) {
            case 1:
                outedType2 = "经典著作";
                break;
            case 2:
                outedType2 = "青春校园";
                break;
            case 3:
                outedType2 = "历史文学";
                break;
        }
        switch (book1.getTypeID3()) {
            case 1:
                outedType3 = "经典著作";
                break;
            case 2:
                outedType3 = "青春校园";
                break;
            case 3:
                outedType3 = "历史文学";
                break;
        }

        switch (book1.getBookState()) {
            case 0:
                outedBookState = "已出库";
                break;
            case 1:
                outedBookState = "未出库";
                break;
        }
        model1.addAttribute("bookID", id);
        model1.addAttribute("outedTypeID1", outedType1);
        model1.addAttribute("outedTypeID2", outedType2);
        model1.addAttribute("outedTypeID3", outedType3);
        model1.addAttribute("outedBookState", outedBookState);
        model1.addAttribute("pic", book1.getPic());
        return "outedBookDetail";
    }


    /**
     * 请求：/toOutedBook -> 已出库书籍页面
     * @param model 封装需要转发到前端的数据
     * @return "outedBook" 转发到已出库书籍页面
     */
    @RequestMapping("/toOutedBook")
    public String toOutedBook(Model model) {
        List<Book> books = bookIOService.queryOutedBook();
        model.addAttribute("list", books);
        return "outedBook";
    }

    /**
     * 请求：/reStoreBook-> 书籍重新入库（回收站恢复数据）
     * @param id 接收前端传来的需要重新入库的书籍ID
     * @return "redirect:/bookIO/toOutedBook" 定位到已出库书籍页面
     */
    @RequestMapping("/reStoreBook")
    public String reStoreBook(int id) {
        bookIOService.reStoreBookByID(id);
        return "redirect:/bookIO/toOutedBook";
    }

    /**
     * 请求：/queryBookByName 通过书籍名称模糊查询书籍所有信息
     * @param model 封装书籍信息
     * @param queryBookName 接收前端传来的需要模糊查询的书籍名称
     * @return "allBook" 转发到所有书籍页面，显示模糊查询到的所有结果
     */
    @RequestMapping("/queryBookByName")
    public String queryBookByName(Model model, String queryBookName) {
        List<Book> books = bookIOService.queryBookByName(queryBookName);
        model.addAttribute("list", books);
        System.out.println("查询结果是books=>" + books);
        if (books.size() == 0) {
            model.addAttribute("err", "未找到");
        }
        return "allIOBook";
    }

    /**
     * 请求：/queryOutedBookByName -> 通过书籍名称模糊查询已出库书籍
     * @param model 封装书籍信息
     * @param queryBookName 接收前端输入的书籍模糊名称
     * @return "outedBook" 转发到已出库书籍页面，显示模糊查询得到的书籍结果
     */
    @RequestMapping("/queryOutedBookByName")
    public String queryOutedBookByName(Model model, String queryBookName) {
        List<Book> books = bookIOService.queryOutedBookByName(queryBookName);
        model.addAttribute("list", books);
        if (books.size() == 0) {
            model.addAttribute("err", "未找到");
        }
        return "outedBook";
    }


    /**
     * 请求：/delBook/{bookID} ->通过书籍ID删除书籍
     * @param id 接收前端传来的需要删除的书籍的ID
     * @return "redirect:/bookIO/toOutedBook" 重定向到已出库书籍页面
     */
    @RequestMapping("/delBook/{bookID}")
    public String delBook(@PathVariable("bookID") int id) {
        bookIOService.deleteBookByID(id);
        return "redirect:/bookIO/toOutedBook";
    }
}
